import socket

sk = socket.socket()

ip_port = ("127.0.0.1", 8080)
sk.bind(ip_port)
sk.listen(5)

while True:
    conn, addr = sk.accept()
    conn.sendall("欢迎光临我爱我家".encode("utf-8"))

    size = conn.recv(1024)
    size_str = str(size, encoding="utf-8")
    file_size = int(size_str)

    conn.sendall("开始传送".encode("utf-8"))

    has_size = 0
    f = open("./img/db_new.jpg", "wb")
    while True:
        if file_size == has_size:
            conn.sendall("上传成功".encode("utf-8"))
            break
        date = conn.recv(1024)
        f.write(date)
        has_size += len(date)

    f.close()
